<template>
  <el-row :gutter="40">
    <el-col v-if="!form.id">
      <el-empty description="请选择左侧菜单后操作" :image-size="100"></el-empty>
    </el-col>
    <template v-else>
      <el-col :lg="12">
        <h2>{{ form.name || "新增菜单" }}</h2>
        <el-form
          :model="form"
          :rules="rules"
          ref="dialogForm"
          label-width="80px"
          label-position="right"
          class="noFlex"
        >
          <el-form-item label="显示名称" prop="name">
            <el-input
              v-model="form.name"
              clearable
              placeholder="菜单显示名字"
              :maxlength="form.fatherId == 0 ? 4 : 8"
              show-word-limit
            ></el-input>
          </el-form-item>
          <el-form-item label="上级菜单" prop="fatherId">
            <el-cascader
              v-model="form.fatherId"
              :options="menuOptions"
              :props="menuProps"
              :show-all-levels="false"
              placeholder="顶级菜单"
              clearable
              disabled
            ></el-cascader>
          </el-form-item>
          <el-form-item label="类型" prop="type">
            <el-radio-group v-model="form.type">
              <!-- <el-radio-button label="menu">菜单</el-radio-button> -->
              <!--  <el-radio-button label="iframe">Iframe</el-radio-button> -->
              <el-radio-button label="view">外链</el-radio-button>
              <!-- <el-radio-button label="button">按钮</el-radio-button> -->
            </el-radio-group>
          </el-form-item>
          <el-form-item label="URL" prop="path" v-if="form.type == 'view'">
            <el-input v-model="form.url" clearable placeholder=""></el-input>
            <div class="el-form-item-msg">如：http://www.baidu.com</div>
          </el-form-item>
          <!-- <el-form-item label="排序" style="width: 100%">
            <el-input-number
              v-model="form.sort"
              controls-position="right"
              :min="0"
            ></el-input-number>
          </el-form-item> -->
          <el-form-item>
            <el-button type="primary" @click="save" :loading="loading"
              >保 存</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </template>
  </el-row>
</template>

<script>
export default {
  props: {
    menu: { type: Object, default: () => { } },
  },
  data () {
    return {
      form: {
        name: '',
        type: 'view',
        url: ''
      },
      menuOptions: [],
      menuProps: {
        value: 'id',
        label: 'name',
        checkStrictly: true
      },
      rules: [],
      loading: false
    }
  },
  watch: {
    menu: {
      handler () {
        this.menuOptions = this.treeToMap(this.menu)
      },
      deep: true
    }
  },
  mounted () {
  },
  methods: {
    //简单化菜单
    treeToMap (tree) {
      const map = []
      tree.forEach(item => {
        var obj = {
          id: item.id,
          fatherId: item.fatherId,
          name: item.name,
          // children: item.children && item.children.length > 0 ? this.treeToMap(item.children) : null
        }
        map.push(obj)
      })
      return map
    },
    //保存
    async save () {
      console.log(this.form)
      this.loading = true
      delete this.form.children
      delete this.form.createTime
      var res = await this.$API.weChat.addMenu.post(this.form)
      this.loading = false
      if (res.code == 200) {
        this.$message.success("保存成功")
      } else {
        this.$message.warning(res.message)
      }
    },
    //表单注入数据
    setData (data) {
      console.log(data)
      this.form = data
    }
  }
}
</script>

<style scoped>
h2 {
  font-size: 17px;
  color: #3c4a54;
  padding: 0 0 30px 0;
}
.apilist {
  border-left: 1px solid #eee;
}

[data-theme="dark"] h2 {
  color: #fff;
}
[data-theme="dark"] .apilist {
  border-color: #434343;
}
</style>
